<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-11-06 16:16:01
 *
 * 项目：/levfm  -  $  - AdminDatasController.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\levfm\controllers;

use Lev;
use modules\levfm\controllers\_gen\BaseAdminDatas;
use modules\levfm\helpers\UrlfmHelper;
use modules\levfm\table\LevfmDatas;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');

class BaseAdminDatasMiddle extends BaseAdminDatas
{

    public static function actionCheck() {
        isset($_GET['status']) || $_GET['status'] = 2;
        parent::actionIndex();
    }

    public static function actionForm()
    {
        if ($opid = Lev::GPv('opid')) {
            parent::redirect(UrlfmHelper::addData($opid));
        }
        return parent::actionForm(); // TODO: Change the autogenerated stub
    }

    public static function headerHtm()
    {
        //return parent::headerHtm(); // TODO: Change the autogenerated stub
        return '<style>.button.admin-create-btnv {display: none;}</style>';
    }

    public static function footerHtm()
    {
        return parent::footerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerHtm</tips>';
    }

    public static function cardFooterButtons($btns = [])
    {
        return parent::cardFooterButtons($btns); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义cardFooterButtons</tips>';
    }

    public static function headerViewHtm()
    {
        return parent::headerViewHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义headerViewHtm：文件位置：'.__DIR__ . '/AdminDatasController.php'.'</tips>';
    }

    public static function footerViewHtm()
    {
        return parent::footerViewHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerViewHtm</tips>';
    }
}

class AdminDatasController extends BaseAdminDatasMiddle
{

    public static $tmpPath = 'admin-datas';
    public static $trash   = true;//是否允许删除数据
    public static $addurl  = true;//是否显示创建数据按钮
    public static $addRoute = 'Form_levfm_datas';//自定义创建数据表单地址 - 默认使用源表单

    public static $srhtitle    = '';//模糊搜索字段名称
    public static $srhkeyWhere = '';//模糊搜索sql语句 例：name LIKE '%{srhkey}%'

    public static $limit = 25;//每页显示多少条
    public static $order = ['id DESC'];//排序

    /**
     * 列表显示字段设置
     * 例：array(
    'id'           => array('order' => null, 'name' => 'Id', 'thattr' => 'wd30 nowrap', 'merge' => []),

    'name'         => array('order' => null, 'name' => '模块名称', 'thattr' => 'wd30 nowrap', 'merge' => ['addtime']),

    'displayorder' => array('order' => null, 'name' => '排序', 'thattr' => 'wd30 nowrap', 'merge' => []),

    'uptime'       => array('order' => null, 'name' => '更新时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

    );
     *
     * order: 点击字段排序
     * name: 字段名称
     * thattr: 表格th属性，放class属性内，内容举例：wd30 nowrap" style="font-size:12px
     * merge: 合并显示，多个字段合并显示到一个上
     *
     * @see BaseAdminDatas::showColumns();
     */
    public static function showColumns() {
        //return parent::showColumns(); // TODO: Change the autogenerated stub
        return array(
                'id'       => array('order' => null, 'name' => 'Id', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'uid'      => array('order' => null, 'name' => '用户UID', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'formid'   => array('order' => null, 'name' => '关联表单ID', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'srhkey'   => array('order' => null, 'name' => '搜索关键字', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'srhtype'  => array('order' => null, 'name' => '分类搜索', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'datas'    => array('order' => null, 'name' => '表单数据', 'thattr' => ' nowrap', 'merge' => []),

                //'rules'    => array('order' => null, 'name' => '表单权限', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'settings' => array('order' => null, 'name' => '通用设置', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'status'   => array('order' => null, 'name' => '状态', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'uptime'   => array('order' => null, 'name' => '更新时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'#opbtns'  => array('order' => null, 'name' => '操作按钮', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'addtime'  => array('order' => null, 'name' => '添加时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

            );
    }

    /**
     *
     return [
        'showtype' => [
            //从上往下先到先得
            'input_{columnKey}'  => ['width'=>120,'textarea'=>1],//显示为无刷新更改字段
            'srhkey_{columnKey}' => ['or'=>['{columnKey}']],//显示为点击可搜索字段
            'status_{columnKey}' => 1,//显示为开关状态字段 status 自动
            'order_{columnKey}'  => 1,//显示为可排序输入框 含order关键字 自动
            'time_{columnKey}'   => 1,//显示为时间格式字段 含time关键字 自动
        ],
    ];
     *
     * @return array
     */
    public static function setColumnShowtype()
    {
        //return parent::setColumnShowtype(); // TODO: Change the autogenerated stub
        return [
            'showtype' => [
                //从上往下先到先得
                'input_{columnKey}'  => ['width'=>120,'textarea'=>1],//显示为无刷新更改字段
                'input_status'  => [],//显示为无刷新更改字段
                'input_srhkey'  => ['width'=>220,],//显示为无刷新更改字段
                'srhkey_{columnKey}' => ['or'=>['{columnKey}']],//显示为点击可搜索字段
                'srhkey_formid' => [],//显示为点击可搜索字段
                'srhkey_status' => [],//显示为点击可搜索字段
                'status_{columnKey}' => 1,//显示为开关状态字段 status 自动
                'order_{columnKey}'  => 1,//显示为可排序输入框 含order关键字 自动
                'time_{columnKey}'   => 1,//显示为时间格式字段 含time关键字 自动
            ],
        ];
    }

    public static function col_status() {
        return LevfmDatas::status();
    }

    public static function col_formid() {
        if (!isset(static::$tempDatas['formids'])) {
            static::$tempDatas['formids'] = [];
            $formids = Lev::getArrayColumn(static::$pages['lists'], ['formid']);
            if ($formids) {
                $result = LevfmForms::findAllField('id,name,settings', "id IN (".implode(',', $formids).")");
                foreach ($result as $v) {
                    static::$tempDatas['formids'][$v['id']] = $v['id'].'@<br>'.$v['name'];
                    $_fieldType = Lev::getSettings(Lev::getSettings($v['settings'], '_fieldType'));
                    if ($_fieldType) {
                        foreach ($_fieldType as $r) {
                            static::$tempDatas['classify'][$v['id']][$r['id']] = $r['id'] . '@' . $r['name'];
                        }
                    }
                }
            }
        }

        return static::$tempDatas['formids'];
    }

    public static function formatListsv($sv, $lists)
    {
        $v = $sv;
        $sv = parent::formatListsv($sv, $lists); // TODO: Change the autogenerated stub
        return $sv;
    }

    public static function formatUid($sv, $lists, $srhkey = ['uid'])
    {
        return parent::formatUid($sv, $lists, $srhkey); // TODO: Change the autogenerated stub
    }

    public static function redirct($columnKey, $sv)
    {
        return parent::redirct($columnKey, $sv); // TODO: Change the autogenerated stub
    }

    public static function optButtons($sv)
    {
        //return parent::optButtons($sv); // TODO: Change the autogenerated stub
        $btns[] = [
            'name' => '浏览数据',
            'link' => UrlfmHelper::views($sv['id']),
            'attr' => 'color-green openPP" title="浏览数据：'.$sv['id'].'" data-full=1 target="_blank" _bk="1'
        ];

        $htms = '<div class="wd60 nowrap flex-box">';
        foreach ($btns as $v) {
            $htms .= '<a class="button button-fill wd100 wdmin '.$v['attr'].'" href="' . $v['link'] . '">'.$v['name'].'</a>';
        }
        return $htms.'</div>&nbsp;';
    }

    public static function footerBtns($btns = [])
    {
        return parent::footerBtns($btns); // TODO: Change the autogenerated stub
    }

}